package anc.tankshooter.ui;

import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.ui.SceneManager.SceneType;

public class ConfirmScreen extends IScreen {
	
	private Sprite mBackground ;
	private Text mInfoText;
	private TiledSprite mTitleSceen;
	private TiledSprite mBtnOk,mBtnCancle;
	private ButtonDialogWatcher mWatcher;
	public ConfirmScreen(String text,ButtonDialogWatcher watcher){
		mWatcher=watcher;
		
		attachChild(mBackground);
		attachChild(mTitleSceen);
		attachChild(mInfoText); mInfoText.setText(text);
		float x = GameActivity.CAMERA_WITDH/2 - 390/2;
		mInfoText.setX(x+mBackground.getWidth()/2-mInfoText.getWidth()/2);
		attachChild(mBtnOk); registerTouchArea(mBtnOk);
		attachChild(mBtnCancle); registerTouchArea(mBtnCancle);
		this.setBackgroundEnabled(false);
	}
	
	@Override
	protected void createScene() {
		float x = GameActivity.CAMERA_WITDH/2 - 390/2;
		float y = GameActivity.CAMERA_HEIGHT/2 - 150/2;
		
		mBackground = new Sprite(x,y,ResourcesManager.getInstance().mPanelTexture,  ResourcesManager.getInstance().mVertexManager);
		mBackground.setSize(390,150);
		
		mTitleSceen = new TiledSprite(x,y,ResourcesManager.getInstance().mDialogTitlesTexture,  ResourcesManager.getInstance().mVertexManager);
		mTitleSceen.setCurrentTileIndex(5);
		
		mInfoText = new Text(x+20, y+50, ResourcesManager.getInstance().mWhiteFont20,"",256, ResourcesManager.getInstance().mVertexManager);
		
		mBtnOk = new TiledSprite(x+90, y+100,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnOk.setCurrentTileIndex(12);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
						mWatcher.onOkClick();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnOk.setCurrentTileIndex(13);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnOk.setCurrentTileIndex(12);
		mBtnCancle = new TiledSprite(x+100+80*1+10, y+100,ResourcesManager.getInstance().mBtnTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP||pSceneTouchEvent.getAction()==TouchEvent.ACTION_OUTSIDE||
						pSceneTouchEvent.getAction()==TouchEvent.ACTION_CANCEL){
					mBtnCancle.setCurrentTileIndex(8);
					if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP) {
						mWatcher.onCancleClick();
					}
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					mBtnCancle.setCurrentTileIndex(9);
				}
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mBtnCancle.setCurrentTileIndex(8);
		
	}

	@Override
	public void onUpdateScreen(float second) {
	}

	@Override
	public boolean onBackKeyPressed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		
	}
		
	public static interface ButtonDialogWatcher{
		public void onOkClick();
		public void onCancleClick();
	}
}
